package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.DownDataVo;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.dto.TemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.dto.vo.SummaryQueryReqVo;
import com.budwk.app.sqfwV2.enums.RedWhiteExcel;
import com.budwk.app.sqfwV2.enums.RedWhitePdf;
import com.budwk.app.sqfwV2.service.dayingRoad.RedWhiteRecordExcelService;
import com.budwk.app.sqfwV2.service.dayingRoad.RedWhiteRecordPdfService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: dfxy-shequ
 * @Author: yangweimin
 * @CreateTime: 2024-01-30  16:11
 * @Description: 大营路红白事备案
 */
@IocBean
@At("/platform/sqfw/redWhiteRecord")
@ApiDefinition(tag = "大营路")
@Slf4j
public class RedWhiteRecordController {

    @Inject
    private RedWhiteRecordPdfService redWhiteRecordPdfService;
    @Inject
    private RedWhiteRecordExcelService redWhiteRecordExcelService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "获取红白事数据")
    @ApiResponses
    @SaCheckLogin
    public Result<?> list(@Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize,
                          @Param("type") String type, @Param("fillDate") String fillDate, @Param("sort") String sort) {
        return redWhiteRecordPdfService.list(pageNo, pageSize, type, fillDate, sort);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新")
    @SaCheckLogin
    public Result<?> update(@Param("..") TemplateCodeUpdateDTO dto) {
        redWhiteRecordPdfService.update(dto);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "数据检测")
    @SaCheckLogin
    public Result<?> checkData(@Param("..") SummaryQueryReqVo reqVo) {

        List<DownFileTypesResVo> downFileTypesResVos = new ArrayList<>();

        // pdf
        DownExcelLedgerReqVo pdfLedgerReqVo = new DownExcelLedgerReqVo();
        pdfLedgerReqVo.setCheckData(true);
        pdfLedgerReqVo.setFillStartDate(reqVo.getFillStartDate());
        pdfLedgerReqVo.setFillEndDate(reqVo.getFillEndDate());
        List<GenerateExcelUrlVO> generatePdfUrlVOList = redWhiteRecordPdfService.downPdfList(pdfLedgerReqVo);
        for (GenerateExcelUrlVO vo : generatePdfUrlVOList) {
            RedWhitePdf pdf = RedWhitePdf.getEnum(vo.getFileIdentity());
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            resVo.setLabel(pdf.getName())
                    .setPreviewImage(List.of(pdf.getImg().split(",")))
                    .setFileCode(String.valueOf(pdf.getCode()))
                    .setCreatedAt(System.currentTimeMillis())
                    .setLocation(Integer.parseInt(pdf.getCode()))
                    .setFileName(pdf.getFileName())
                    .setFileType("pdf")
                    .setHiveData(vo.getCreateConditionFlag()==null?false:vo.getCreateConditionFlag());
            downFileTypesResVos.add(resVo);
        }

        // excel
        DownExcelLedgerReqVo excelLedgerReqVo = new DownExcelLedgerReqVo();
        excelLedgerReqVo.setCheckData(true);
        excelLedgerReqVo.setFillStartDate(reqVo.getFillStartDate());
        excelLedgerReqVo.setFillEndDate(reqVo.getFillEndDate());
        DownDataVo<GenerateExcelUrlVO> downDataVo = redWhiteRecordExcelService.createExcel(excelLedgerReqVo);
        List<GenerateExcelUrlVO> generateExcelUrlVOList = downDataVo.getVoList();
        for (GenerateExcelUrlVO vo : generateExcelUrlVOList) {
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            RedWhiteExcel excel = RedWhiteExcel.getEnum(vo.getFileIdentity());
            resVo.setLabel(excel.getName())
                    .setPreviewImage(List.of(excel.getImg().split(",")))
                    .setFileCode(String.valueOf(excel.getCode()))
                    .setCreatedAt(System.currentTimeMillis())
                    .setLocation(Integer.parseInt(excel.getCode()))
                    .setFileName(excel.getFileName())
                    .setFileType("excel")
                    .setHiveData(vo.getCreateConditionFlag()==null?false:vo.getCreateConditionFlag());
            downFileTypesResVos.add(resVo);
        }
        return Result.data(NutMap.NEW().addv("list", downFileTypesResVos).addv("totalCount", downDataVo.getTotalCount()));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载pdf")
    @SaCheckLogin
    public Result<?> downPdf(@Param("..") DownExcelLedgerReqVo reqVo) {
        DownExcelLedgerReqVo pdfLedgerReqVo = new DownExcelLedgerReqVo();
        pdfLedgerReqVo.setFileCode(reqVo.getFileCode());
        pdfLedgerReqVo.setFillStartDate(reqVo.getFillStartDate());
        pdfLedgerReqVo.setFillEndDate(reqVo.getFillEndDate());
        pdfLedgerReqVo.setDownTemplate(reqVo.isDownTemplate());
        List<GenerateExcelUrlVO> generatePdfUrlVOList = redWhiteRecordPdfService.downPdfList(pdfLedgerReqVo);
        return Result.data(generatePdfUrlVOList);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "下载excel")
    @SaCheckLogin
    public Result<?> downExcel(@Param("..") DownExcelLedgerReqVo reqVo) {
        DownExcelLedgerReqVo pdfLedgerReqVo = new DownExcelLedgerReqVo();
        pdfLedgerReqVo.setFileCode(reqVo.getFileCode());
        pdfLedgerReqVo.setFillStartDate(reqVo.getFillStartDate());
        pdfLedgerReqVo.setFillEndDate(reqVo.getFillEndDate());
        pdfLedgerReqVo.setDownTemplate(reqVo.isDownTemplate());
        DownDataVo<GenerateExcelUrlVO> downDataVo = redWhiteRecordExcelService.createExcel(pdfLedgerReqVo);
        return Result.data(downDataVo.getVoList());
    }
}
